# -*- coding: utf-8 -*-
# _author: zb
# date: 19-4-25 下午5:46


from collections import OrderedDict
from django.conf import settings
from django.template import loader
import os
import time

from answers.serializers import QuestCommentSerializer, LabelQuestSerializer
from .models import QuesModel, QuesLabelModel


def generate_static_answers_detail_html(ques_id):
    """生成页面静态化html"""
    print('%s:generate_static_answers_detail_html' %time.ctime())
    # 通过序列化器返回相关的数据
    ser = QuestCommentSerializer(QuesModel.objects.get(id=ques_id))
    a = ser.data

    label_id = a['label']['id']
    print(label_id)

    similarques = LabelQuestSerializer(QuesLabelModel.objects.filter(label_id=label_id)[:5],many=True)
    print(similarques.data)
    context = {
        'ques_data':ser.data,
        'label_data': similarques.data,
    }
    template = loader.get_template('answers/qa-detail.html')
    html_text = template.render(context)
    file_path = os.path.join(settings.GENERATED_STATIC_HTML_FILES_DIR,  'answers_'+str(ques_id)+'.html')
    with open(file_path,'w') as f:
        f.write(html_text)

